@file:JvmName("LifecycleThread")

package hos.thread.lifecycle

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import hos.thread.RunnableJob
import hos.thread.ThreadOption
import hos.thread.hander.MainHandler

/**
 * <p>Title: MHLifeKt </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @date : 2023/7/28 8:39
 * @version : 1.0
 */

inline fun <T> getLifecycleJob(
    crossinline block: RunnableJob<T>.() -> T,
) = LifecycleRunJob.getLifecycle<T> {
    block(this)
}

@JvmOverloads
inline fun <T> getLifecycleJobLive(
    info: MutableLiveData<LifecycleThreadResult> = MutableLiveData<LifecycleThreadResult>(),
    crossinline block: RunnableJob<T>.() -> T,
) = LifecycleRunJobLive.getLifecycleLive<T>(info) {
    block(this)
}

@JvmOverloads
inline fun LifecycleOwner.runLifecycleUI(
    handle: ThreadOption.UI = ThreadOption.UI.ON,
    time: Long = 400,
    crossinline block: MainHandler.() -> Unit,
) = LifecycleHandler.runLifecycle(this, handle, time) {
    block.invoke(this)
}

@JvmOverloads
inline fun <T> LifecycleOwner.runLifecycleJob(
    job: ThreadOption.Job = ThreadOption.Job.ON,
    crossinline block: (RunnableJob<T>.() -> T),
) = LifecycleRunJob.runLifecycle<T>(this, job) {
    block(this)
}

@JvmOverloads
inline fun <T> LifecycleOwner.runLifecycleJobLive(
    job: ThreadOption.Job = ThreadOption.Job.ON,
    info: MutableLiveData<LifecycleThreadResult> = MutableLiveData<LifecycleThreadResult>(),
    crossinline block: (RunnableJob<T>.() -> T),
) = LifecycleRunJobLive.runLifecycleLive<T>(this, job, info) {
    block(this)
}

inline fun <reified T> MutableLiveData<LifecycleThreadResult>.runLifecycleJobLive(
    crossinline block: RunnableJob<T>.() -> T,
) = LifecycleRunJobLive.getLifecycleLive<T>(this) {
    block(this)
}

